package ynu.elm.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ynu.elm.entity.OrderCreationRequest;
import ynu.elm.entity.Orders;
import ynu.elm.service.OrdersService;
import java.util.List;

@RestController
@RequestMapping(value = "/OrdersController")
public class OrdersController {

    @Resource
    OrdersService ordersService;

    @PostMapping(value = "/createOrders")
    public int createOrders(@RequestBody OrderCreationRequest request){
        return ordersService.createOrders(request);
    }

    @GetMapping(value = "/getOrdersById")
    public Orders getOrdersById(@RequestParam(value = "orderId") Integer orderId){
        return ordersService.getOrdersById(orderId);
    }

    @GetMapping(value = "/listOrdersByUserId")
    public List<Orders> listOrdersByUserId(@RequestParam(value = "userId") String userId){
        return ordersService.listOrdersByUserId(userId);
    }

    @PutMapping("/updateOrderState")
    public int updateOrderState(@RequestParam(value = "orderId") Integer orderId,
                                @RequestParam(value = "orderState") Integer orderState){
        System.out.println("Updating order: " + orderId + " to state: " + orderState);
        Orders order = ordersService.getOrdersById(orderId);
        if (order != null) {
            order.setOrderState(orderState);
            return ordersService.updateOrder(order);
        }
        return 0;
    }
}